<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 行程订单 开发者: 麦沃德科技-半夏
+---------------------------------------------------------------------- -->

<template>
	<page-meta :page-style="'overflow:' + (pageShow ? 'hidden' : 'visible')"></page-meta>
	<view class="container" :style="{'--theme-color': themeColor}" v-if="loadEnd">
		<!-- 顶部导航 -->
		<view class="container-screen">
			<scroll-view scroll-x style="white-space: nowrap;">
				<view class="screen-item" v-for="(item, index) in screenList" :key="index" @click="changeScreen(item.state)">
					<view class="text" :class="{active: selectScreen == item.state}">{{item.name}}</view>
				</view>
			</scroll-view>
		</view>
		<!-- 订单列表 -->
		<view class="container-main">
			<order-list :show-data="orderList" :loading="orderLoading" @getOrderList="resetOrderList" @onCancel="handleCancel" @onEvaluate="handleEvaluate" v-if="orderList.length"></order-list>
			<empty top="36%" title="暂无相关内容~" v-else></empty>
		</view>
		<!-- 取消订单弹窗 -->
		<cancel-order-modal ref="cancelOrderModal" @onChange="pageChange" @onUpdate="resetOrderList"></cancel-order-modal>
		<!-- 评价订单弹窗 -->
		<evaluate-order-modal ref="evaluateOrderModal" @onChange="pageChange" @onUpdate="resetOrderList"></evaluate-order-modal>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	import orderList from "@/pages/passenger/component/order/list.vue"
	import cancelOrderModal from "@/pages/passenger/component/modal/cancel-order.vue"
	import evaluateOrderModal from "@/pages/passenger/component/modal/evaluate-order.vue"
	export default {
		components: {
			orderList,
			cancelOrderModal,
			evaluateOrderModal,
		},
		data() {
			return {
				// 页面是否阻止滚动
				pageShow: false,
				// 加载完成
				loadEnd: false,
				// 分类列表
				screenList: [{
						name: "全部",
						state: "",
					},
					{
						name: "待接单",
						state: "pending",
					},
					{
						name: "行程中",
						state: "in_progress",
					},
					{
						name: "已完成",
						state: "completed",
					},
					{
						name: "已取消",
						state: "cancelled",
					},
				],
				// 已选分类
				selectScreen: "",
				// 订单列表
				orderList: [],
				// 分类查询参数
				page: 1,
				limit: 10,
				hasMore: false,
				// 订单列表是否加载
				orderLoading: false,
			}
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
				shareTitle: state => state.app.shareTitle,
				shareImage: state => state.app.shareImage,
			})
		},
		onLoad(option) {
			this.selectScreen = option.state || ""
			uni.showLoading({
				title: "加载中"
			})
			this.getOrderList(() => {
				uni.hideLoading()
				this.loadEnd = true
			})
		},
		onShow() {
			if (this.loadEnd) {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				});
				this.page = 1
				this.orderLoading = true
				this.getOrderList(() => {
					this.orderLoading = false
				})
			}
		},
		onPullDownRefresh() {
			this.page = 1
			this.orderLoading = true
			this.getOrderList(() => {
				this.orderLoading = false
				uni.stopPullDownRefresh()
			})
		},
		onReachBottom() {
			if (this.hasMore) {
				this.page++
				this.getOrderList()
			}
		},
		onShareAppMessage() {
			return {
				title: this.shareTitle,
				imageUrl: this.shareImage,
			}
		},
		onShareTimeline() {
			return {
				title: this.shareTitle,
				imageUrl: this.shareImage,
			}
		},
		methods: {
			// 改变页面滚动状态
			pageChange(state) {
				this.pageShow = state
			},
			// 更改分类
			changeScreen(state) {
				this.selectScreen = state
				this.page = 1
				this.orderLoading = true
				this.getOrderList(() => {
					this.orderLoading = false
				})
			},
			// 获取订单列表
			getOrderList(fn) {
				this.$util.request("passenger.order.list", {
					page: this.page,
					limit: this.limit,
					state: this.selectScreen,
				}).then(res => {
					if (fn) fn()
					if (res.code == 1) {
						let list = res.data.data
						this.hasMore = this.page < res.data.total / this.limit ? true : false
						this.orderList = this.page == 1 ? list : [...this.orderList, ...list];
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					if (fn) fn()
					console.error('获取订单列表 ', error)
				})
			},
			// 重置订单列表
			resetOrderList() {
				this.page = 1
				this.orderLoading = true
				this.getOrderList(() => {
					this.orderLoading = false
				})
			},
			// 取消订单
			handleCancel(data) {
				this.$refs.cancelOrderModal.open(JSON.stringify(data))
			},
			// 评价订单
			handleEvaluate(id, state) {
				this.$refs.evaluateOrderModal.open({ id, state })
			},
		}
	}
</script>

<style lang="scss">
	.container {
		.container-screen {
			background: #ffffff;
			position: sticky;
			top: 0;
			z-index: 99;
			padding: 0 32rpx;

			.screen-item {
				min-width: 20%;
				padding: 0 24rpx;
				display: inline-flex;
				justify-content: center;

				.text {
					padding: 28rpx 0 20rpx;
					color: #242629;
					font-size: 28rpx;
					line-height: 40rpx;
					text-align: center;
					border-bottom: 8rpx solid transparent;

					&.active {
						color: var(--theme-color);
						border-color: var(--theme-color);
					}
				}
			}
		}

		.container-main {
			padding: 32rpx;
		}
	}
</style>